package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

/**
 * ClassName: CommonController
 * Package: com.sky.controller.admin
 * Description:
 *
 * @Author Abola
 * @Create 2025/4/16 11:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 阿里云文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传：{},file");

        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //获取原始文件名的后缀   dfdfdf.png
            String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构建新文件名称
            String objectName = UUID.randomUUID().toString() + extention;
            //文件请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}",e);
        }
        return Result.success(MessageConstant.UPLOAD_FAILED);

//        return null;
    }
//    private static final String FILE_UPLOAD_PATH = "C:/Users/Administrator/Desktop/waimai/ziliao/day01/后端初始工程/sky-take-out/sky-server/src/main/resources/upload";
//
//    /**
//     * 本地文件上传
//     * @param file
//     * @return
//     */
//    @PostMapping("/upload")
//    @ApiOperation("文件上传")
//    @ResponseBody
//    public Result<String> uploadfile(@RequestParam("file") MultipartFile file) throws IOException {
//        if (file.isEmpty()) {
//            return Result.error("文件不能为空");
//        }
//
//        File dir = new File(FILE_UPLOAD_PATH);
//        if (!dir.exists() || !dir.isDirectory()) {
//            boolean created = dir.mkdirs();
//            if(created) {
//                log.info("创建文件夹成功: {}", FILE_UPLOAD_PATH);
//            } else {
//                log.warn("创建文件夹失败或已经存在: {}", FILE_UPLOAD_PATH);
//            }
//        }
//
//        String originalFilename = file.getOriginalFilename();
//        if (originalFilename == null || originalFilename.isEmpty()) {
//            return Result.error("文件名无效");
//        }
//        // 获取后缀
//        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//        if (!extension.equalsIgnoreCase(".png") && !extension.equalsIgnoreCase(".jpg") && !extension.equalsIgnoreCase(".jpeg")) {
//            return Result.error("文件格式不支持");
//        }
//        // 拼接新的随机名称
//        originalFilename = UUID.randomUUID().toString() + extension;
//
//
//        // 确保文件路径安全，避免路径遍历攻击
//        Path targetLocation = Paths.get(FILE_UPLOAD_PATH).resolve(originalFilename).normalize();
//        try {
//            Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
//            log.info("文件上传成功: {}", originalFilename);
//        } catch (IOException e) {
//            log.error("文件上传失败: {}", originalFilename, e);
//            return Result.error(MessageConstant.UPLOAD_FAILED);
//        }
//
//        // 你可以根据实际情况调整返回的文件访问链接
//        String fileUrl = "https://localhost:8080/static/" + originalFilename;
//        return Result.success(fileUrl);
//    }
}

